package com.yidu.labyrinth.dao;

import com.yidu.labyrinth.main.LabyrinthModuleEntity;
import com.yidu.labyrinth.main.LabyrinthModuleOrRoleEntity;
import com.yidu.labyrinth.vo.LabyrinthVo;
import com.yidu.labyrinth.vo.Pages;
import org.hibernate.HibernateException;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.util.StringUtils;

import javax.persistence.OneToMany;
import java.util.List;

/**
 * @author 朱星润
 * @类的描述——
 * @date 2019/09/26上午 10:27
 */
public class LabyrinthModuleDaoImpl implements LabyrinthModuleDao {
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public int save(LabyrinthModuleEntity model) {
        try {
            hibernateTemplate.clear();
            hibernateTemplate.saveOrUpdate(model);
        } catch (HibernateException e) {
            return 0;
        }

        return 1;
    }

   @Override
    public List<LabyrinthModuleEntity> findAll(LabyrinthModuleEntity model, Pages pages) {
        //查询模块表
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthModuleEntity.class);

        //条件搜索
        //按模块名称模糊搜索
        if (!StringUtils.isEmpty(model.getModuleName())) {
            criteria.add(Restrictions.ilike("moduleName", "%" + model.getModuleName() + "%"));
        }
        List<LabyrinthModuleEntity> list = (List<LabyrinthModuleEntity>) hibernateTemplate.findByCriteria(criteria, pages.getOutPage(), pages.getLimit());
        return list;
    }

    @Override
    public LabyrinthModuleEntity findById(String id) {
        LabyrinthModuleEntity model = hibernateTemplate.get(LabyrinthModuleEntity.class, id);
        return model;
    }

    @Override
    public Long count(LabyrinthVo vo) {

        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthModuleEntity.class);

        criteria.setProjection(Projections.rowCount());//count(*)

        Long count = (Long) hibernateTemplate.findByCriteria(criteria).get(0);

        return count;
    }

    /**
     * 查询所有不分页
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<LabyrinthModuleEntity> findAllModule() {
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthModuleEntity.class);
        criteria.add(Restrictions.eq("moduleResult",1));
        return (List<LabyrinthModuleEntity>) hibernateTemplate.findByCriteria(criteria);
    }

    /**
     * 查询所有不分页
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<LabyrinthModuleEntity> X_admin() {
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthModuleEntity.class);
        criteria.add(Restrictions.eq("moduleResult",1));
        return (List<LabyrinthModuleEntity>) hibernateTemplate.findByCriteria(criteria);
    }
}
